/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import com.google.common.collect.Sets;

@MappedSuperclass
public abstract class HibernateStep implements Step, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2499787604145302072L;
	private Date dueDate;
	private Date finishDate;
	private Date startDate;
	private String caller;
	private String owner;
	private String status;
	private int actionId;
	private int stepId;
	protected long id = -1;
	private HibernateWorkflowEntry entry;
	protected Set<Long> previousSteps = Sets.newHashSet();
	
	// ~ Constructors
	// ///////////////////////////////////////////////////////////

	public HibernateStep() {
	}

	public HibernateStep(HibernateStep step) {
		this.actionId = step.getActionId();
		this.caller = step.getCaller();
		this.finishDate = step.getFinishDate();
		this.dueDate = step.getDueDate();
		this.startDate = step.getStartDate();

		// do not copy this value, it's for unsaved-value
		//this.id = step.getId();
		this.owner = step.getOwner();
		this.status = step.getStatus();
		this.stepId = step.getStepId();
		this.previousSteps = step.previousSteps;
        this.entry = step.entry;
		
	}

	// ~ Methods
	// ////////////////////////////////////////////////////////////////

	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	@Column(name = "action_id")
	public int getActionId() {
		return actionId;
	}

	public void setCaller(String caller) {
		this.caller = caller;
	}

	public String getCaller() {
		return caller;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	@Column(name = "due_Date")
	public Date getDueDate() {
		return dueDate;
	}

	

	public void setFinishDate(Date finishDate) {
		this.finishDate = finishDate;
	}

	@Column(name = "finish_Date")
	public Date getFinishDate() {
		return finishDate;
	}

	public void setId(long id) {
		this.id = id;
	}

	

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getOwner() {
		return owner;
	}

	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@Column(name = "start_Date")
	public Date getStartDate() {
		return startDate;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return status;
	}

	public void setStepId(int stepId) {
		this.stepId = stepId;
	}

	@Column(name = "step_Id")
	public int getStepId() {
		return stepId;
	}
	
	public void setEntry(HibernateWorkflowEntry entry) {
		this.entry = entry;
	}

	@ManyToOne(targetEntity=HibernateWorkflowEntry.class)
	@JoinColumn(name = "entry_Id", referencedColumnName = "id")	
	public HibernateWorkflowEntry getEntry() {
		return entry;
	}

	@Transient
	public long getEntryId() {
		return entry.getId();
	}
	
	@Transient
	public long[] getPreviousStepIds() {
		if (previousSteps == null) {
			return new long[0];
		}

		long[] previousStepIds = new long[previousSteps.size()];
		int i = 0;

		for (Iterator<Long> iterator = previousSteps.iterator(); iterator.hasNext();) {
			Long hibernateStep = iterator.next();
			previousStepIds[i] = hibernateStep;
			i++;
		}

		return previousStepIds;
	}
	public void setPreviousSteps(Set<Long> previousSteps) {
		this.previousSteps = previousSteps;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HibernateStep other = (HibernateStep) obj;
		if (id != other.id)
			return false;
		return true;
	}	
	
}
